{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1f20875-c50d-46df-906b-49ec1d0c6002",
   "metadata": {},
   "source": [
    "## 9.7 保存和加载模型参数\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "\n",
    "训练手写数字网络，分别以HDF5格式和SavedModel格式保存模型。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f5b4fc39-cbcf-432a-bf1e-e75e642d4b87",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c1d0295a-4ac4-470a-8263-027a3d69ac2c",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n",
    "使用Sequential搭建一个多层神经网络来训练模型权值参数，使用model.save_ weights方法即可将训练出来的权值参数保存到本地。\n",
    "\n",
    "在加载本地的模型权值参数之前，需要创建一个与之匹配的网络，如训练出该模型参数的网络结构为输入层有784个神经元，隐含层有128个神经元，输出层有10个神经元，那么用于加载该模型权值参数的网络也应该定义成相同的结构。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/5\n",
      "938/938 [==============================] - 1s 1ms/step - loss: 0.2929 - sparse_categorical_accuracy: 0.9172 - val_loss: 0.1464 - val_sparse_categorical_accuracy: 0.9576\n",
      "Epoch 2/5\n",
      "938/938 [==============================] - 1s 1ms/step - loss: 0.1288 - sparse_categorical_accuracy: 0.9625 - val_loss: 0.1057 - val_sparse_categorical_accuracy: 0.9678\n",
      "Epoch 3/5\n",
      "938/938 [==============================] - 1s 1ms/step - loss: 0.0896 - sparse_categorical_accuracy: 0.9732 - val_loss: 0.0909 - val_sparse_categorical_accuracy: 0.9726\n",
      "Epoch 4/5\n",
      "938/938 [==============================] - 1s 1ms/step - loss: 0.0688 - sparse_categorical_accuracy: 0.9795 - val_loss: 0.0866 - val_sparse_categorical_accuracy: 0.9728\n",
      "Epoch 5/5\n",
      "938/938 [==============================] - 1s 1ms/step - loss: 0.0535 - sparse_categorical_accuracy: 0.9840 - val_loss: 0.0784 - val_sparse_categorical_accuracy: 0.9750\n",
      "Model: \"sequential_3\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " flatten_3 (Flatten)         (None, 784)               0         \n",
      "                                                                 \n",
      " dense_6 (Dense)             (None, 128)               100480    \n",
      "                                                                 \n",
      " dense_7 (Dense)             (None, 10)                1290      \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 101,770\n",
      "Trainable params: 101,770\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n",
      "1/1 [==============================] - 0s 31ms/step\n",
      "预测值： 2\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGdCAYAAABU0qcqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAbxklEQVR4nO3df3RU9f3n8dcEyIiaTIwhmUQCJiiiArFFSbMqxZIlxLN+Qdku/uguuC4uNLhFtHriUZHq95sWt+rRpfLHtlDPEX/QFTj6tbgYTFhtwBJhKUfNEjaWuCRBWTITgoSQfPYP1qkDCfQOM3nnx/Nxzj2HzNxP7ru3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AATBAgAYIIAAQBMECAAgAkCBAAwQYAAACYIEADABAECAJggQAAAEwQIAGCCm5EiZrHcWPSj615PwCTxs7o13/Oa52r+pec1l4/9yvOa/37NW57X3JPS5HmNJP3jggzPa/If5Wak8IYrIACACQIEADAR9wA99dRT8vl8UduECRPifRgAwACXkPeArr32Wr3//vt/O8hw3moCAERLSBmGDx+uYDCYiG8NABgkEvIe0L59+5STk6P8/Hzdc889OnDgQK/7dnR0KBwOR20AgMEv7gEqLCzU2rVrtXnzZr388stqaGjQzTffrLa2th73r6ioUCAQiGy5ubnxHgkA0A/FPUClpaX68Y9/rMmTJ6ukpETvvvuuWltb9eabb/a4f3l5uUKhUGRrbGyM90gAgH4o4Z8OSEtL0/jx41VfX9/j836/X36/P9FjAAD6mYT/HNDRo0e1f/9+ZWdnJ/pQAIABJO4Bevjhh1VdXa0vvvhCf/rTn3T77bdr2LBhuuuuu+J9KADAABb3f4L78ssvddddd+nw4cMaNWqUbrrpJm3fvl2jRo2K96EAAANY3AP0+uv9+2aTONPJGVNiWre1YFUMq0Z4XvHCkfGe13ww73rPayRJBw95XjL+yE7Pa5IuuMDzmn/aMcnzmscy/uJ5jSSdvORkTOsAL7gXHADABAECAJggQAAAEwQIAGCCAAEATBAgAIAJAgQAMEGAAAAmCBAAwAQBAgCYIEAAABMECABgIuG/kA7939HLkmNalxTD319iubFo1T94vwln1/+u87ymL9Wv+J7nNevSfx3DkWL7ZY+jN/N3UyQerzIAgAkCBAAwQYAAACYIEADABAECAJggQAAAEwQIAGCCAAEATBAgAIAJAgQAMEGAAAAmCBAAwAQBAgCY4G7YUNorNTGt+9c7f+J5je9I2POak01feF7T3/2HW9/3vObipNjubA30V1wBAQBMECAAgAkCBAAwQYAAACYIEADABAECAJggQAAAEwQIAGCCAAEATBAgAIAJAgQAMEGAAAAmuBkpYtb16f+yHqFf+OIfizyvuS/tP8dwpAs8r3io6QcxHEdKef8zz2u6YjoShjKugAAAJggQAMAEAQIAmCBAAAATBAgAYIIAAQ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WrTIjRkzxm3dutXt3LnTFRUVuaKiIsOp4+9c56G+vt794he/cDt37nQNDQ1u06ZNLj8/302bNs148mgDIkDOOffSSy+5MWPGuOTkZDd16lS3fft265H63Lx581x2drZLTk52l112mZs3b56rr6+3HivhPvjgAyfpjG3+/PnOuVMfxX7iiSdcVlaW8/v9bsaMGa6urs526AQ423k4duyYmzlzphs1apQbMWKEGzt2rFu4cOGg+0taT//7Jbk1a9ZE9vnmm2/cT3/6U3fJJZe4Cy+80N1+++2uqanJbugEONd5OHDggJs2bZpLT093fr/fXXHFFe7nP/+5C4VCtoOfhl/HAAAw0e/fAwIADE4ECABgggABAEwQIACACQIEADBBgAAAJggQAMAEAQIAmCBAAAATBAgAYIIAAQBMECAAgIn/B23zqySm7p5BAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import os\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "# 加载数据\n",
    "(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n",
    "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
    "# 创建网络\n",
    "model1=tf.keras.Sequential()\n",
    "model1.add(tf.keras.layers.Flatten(input_shape=(28,28)))\n",
    "model1.add(tf.keras.layers.Dense(128,activation=\"relu\"))\n",
    "model1.add(tf.keras.layers.Dense(10,activation=\"softmax\"))\n",
    "# 配置网络\n",
    "model1.compile(optimizer='adam',\n",
    "              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),\n",
    "              metrics=['sparse_categorical_accuracy'])\n",
    "model1.fit(\n",
    "    x_train, y_train, \n",
    "    batch_size=64, \n",
    "    epochs=5, \n",
    "    validation_data=(x_test, y_test), \n",
    "    validation_freq=1\n",
    ")\n",
    "model1.summary()\n",
    "# 以HDF5格式保存模型参数\n",
    "model1.save_weights(filepath=\"mnist_weights.h5\",overwrite=True,save_format=None)\n",
    "# 创建模型\n",
    "model2=tf.keras.Sequential()\n",
    "model2.add(tf.keras.layers.Flatten(input_shape=(28,28)))\n",
    "model2.add(tf.keras.layers.Dense(128,activation=\"relu\"))\n",
    "model2.add(tf.keras.layers.Dense(10,activation=\"softmax\"))\n",
    "\n",
    "# 配置网络\n",
    "model2.compile(optimizer='adam',\n",
    "              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),\n",
    "              metrics=['sparse_categorical_accuracy'])\n",
    "# 加载模型\n",
    "model2.load_weights(\"mnist_weights.h5\")\n",
    "img=x_test[1]\n",
    "plt.imshow(img)\n",
    "img=tf.reshape(img,(1,28,28))\n",
    "result=model2.predict(img)\n",
    "num=np.argmax(result)\n",
    "print(\"预测值：\",num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d6393d1a-eae7-42bc-8aba-467deb6a607a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 0s 47ms/step\n",
      "预测值： 2\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGdCAYAAABU0qcqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAbxklEQVR4nO3df3RU9f3n8dcEyIiaTIwhmUQCJiiiArFFSbMqxZIlxLN+Qdku/uguuC4uNLhFtHriUZHq95sWt+rRpfLHtlDPEX/QFTj6tbgYTFhtwBJhKUfNEjaWuCRBWTITgoSQfPYP1qkDCfQOM3nnx/Nxzj2HzNxP7ru3c3xymcmNzznnBABAH0uyHgAAMDQRIACACQIEADBBgAAAJggQAMAEAQIAmCBAAAATBAgAYGK49QCn6+7u1sGDB5WSkiKfz2c9DgDAI+ec2tralJOTo6Sk3q9z+l2ADh48qNzcXOsxAADnqbGxU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9wA99dRT8vl8UduECRPifRgAwACXkPeArr32Wr3//vt/O8hw3moCAERLSBmGDx+uYDCYiG8NABgkEvIe0L59+5STk6P8/Hzdc889OnDgQK/7dnR0KBwOR20AgMEv7gEqLCzU2rVrtXnzZr388stqaGjQzTffrLa2th73r6ioUCAQiGy5ubnxHgkA0A/FPUClpaX68Y9/rMmTJ6ukpETvvvuuWltb9eabb/a4f3l5uUKhUGRrbGyM90gAgH4o4Z8OSEtL0/jx41VfX9/j836/X36/P9FjAAD6mYT/HNDRo0e1f/9+ZWdnJ/pQAIABJO4Bevjhh1VdXa0vvvhCf/rTn3T77bdr2LBhuuuuu+J9KADAABb3f4L78ssvddddd+nw4cMaNWqUbrrpJm3fvl2jRo2K96EAAANY3AP0+uv9+2aTONPJGVNiWre1YFUMq0Z4XvHCkfGe13ww73rPayRJBw95XjL+yE7Pa5IuuMDzmn/aMcnzmscy/uJ5jSSdvORkTOsAL7gXHADABAECAJggQAAAEwQIAGCCAAEATBAgAIAJAgQAMEGAAAAmCBAAwAQBAgCYIEAAABMECABgIuG/kA7939HLkmNalxTD319iubFo1T94vwln1/+u87ymL9Wv+J7nNevSfx3DkWL7ZY+jN/N3UyQerzIAgAkCBAAwQYAAACYIEADABAECAJggQAAAEwQIAGCCAAEATBAgAIAJAgQAMEGAAAAmCBAAwAQBAgCY4G7YUNorNTGt+9c7f+J5je9I2POak01feF7T3/2HW9/3vObipNjubA30V1wBAQBMECAAgAkCBAAwQYAAACYIEADABAECAJggQAAAEwQIAGCCAAEATBAgAIAJAgQAMEGAAAAmuBkpYtb16f+yHqFf+OIfizyvuS/tP8dwpAs8r3io6QcxHEdKef8zz2u6YjoShjKugAAAJggQAMAEAQIAmCBAAAATBAgAYIIAAQBMECAAgAkCBAAwQYAAACYIEADABAECAJggQAAAE9yMFPiO1n/r/caiH/077zcWDSR5v7FoTccwz2t2P/M9z2skaWT445jWAV5wBQQAMEGAAAAmPAdo27Ztuu2225STkyOfz6eNGzdGPe+c05NPPqns7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gAyfpjG3+/PnOuVMfxX7iiSdcVlaW8/v9bsaMGa6urs526AQ423k4duyYmzlzphs1apQbMWKEGzt2rFu4cOGg+0taT//7Jbk1a9ZE9vnmm2/cT3/6U3fJJZe4Cy+80N1+++2uqanJbugEONd5OHDggJs2bZpLT093fr/fXXHFFe7nP/+5C4VCtoOfhl/HAAAw0e/fAwIADE4ECABgggABAEwQIACACQIEADBBgAAAJggQAMAEAQIAmCBAAAATBAgAYIIAAQBMECAAgIn/B23zqySm7p5BAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 加载模型\n",
    "model2.load_weights(\"mnist_weights.h5\")\n",
    "img=x_test[1]\n",
    "plt.imshow(img)\n",
    "img=tf.reshape(img,(1,28,28))\n",
    "result=model2.predict(img)\n",
    "num=np.argmax(result)\n",
    "print(\"预测值：\",num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6044c99-0741-4378-b2b6-f60c293cc3a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
